package com.zyl.points;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.zyl.points.bean.Question;
import com.zyl.points.bean.Task;
import com.zyl.points.service.QuestionService;
import com.zyl.points.service.TaskService;
import com.zyl.points.util.DBHelper;
import com.zyl.points.util.FileUtil;

public class Main extends Activity {
	/**   study_id  学习 
	 *    test_id 测试 
	 *    plan_id 计划 
	 *    data_id 词库 
	 *    set_id  设置  
	 *    about_id  关于 
	 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initResource();
        
        TextView  study_id = (TextView)findViewById(R.id.study_id); 
        TextView  test_id  = (TextView)findViewById(R.id.test_id ); 
        TextView  task_id  = (TextView)findViewById(R.id.task_id ); 
        TextView  data_id  = (TextView)findViewById(R.id.data_id ); 
        TextView  set_id   = (TextView)findViewById(R.id.set_id  ); 
        TextView  about_id = (TextView)findViewById(R.id.about_id); 
   
        
        
        study_id.setOnClickListener(
        new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();  
				 intent.setClass(Main.this,Study.class);  
				 Main.this.startActivity(intent);  
			}
        });
        test_id.setOnClickListener(
        		new OnClickListener(){
        			@Override
        			public void onClick(View arg0) {
        				Intent intent = new Intent();  
       				 intent.setClass(Main.this,Test.class);  
       				 Main.this.startActivity(intent);  
        			}
        		});
        task_id.setOnClickListener(
        		new OnClickListener(){
        			@Override
        			public void onClick(View arg0) {
        				Intent intent = new Intent();  
          				 intent.setClass(Main.this,Plan.class);  
          				 Main.this.startActivity(intent);  ;
        			}
        		});
        data_id.setOnClickListener(
        		new OnClickListener(){
        			@Override
        			public void onClick(View arg0) {
        				Intent intent = new Intent(); 
         				 intent.setClass(Main.this,FileExplorer.class);  
         				 Main.this.startActivity(intent);  ;
        			}
        		});
        set_id.setOnClickListener(
        		new OnClickListener(){
        			@Override
        			public void onClick(View arg0) {
        		        Dialog alertDialog = new AlertDialog.Builder(Main.this). 
        		                setTitle("重置数据"). 
        		                setMessage("您确定重置数据？"). 
        		                setPositiveButton("确定", new DialogInterface.OnClickListener() { 
        		                    @Override 
        		                    public void onClick(DialogInterface dialog, int which) { 
        		                    	new	TaskService(Main.this).reset();
        		                    } 
        		                }). 
        		                setNegativeButton("取消", new DialogInterface.OnClickListener() { 
        		                     
        		                    @Override 
        		                    public void onClick(DialogInterface dialog, int which) { 
        		                    } 
        		                }).create(); 
        		        alertDialog.show(); 
        			}
        		});
        about_id.setOnClickListener(
        		new OnClickListener(){
        			@Override
        			public void onClick(View arg0) {
        				Toast toast = Toast.makeText(getApplicationContext(), "作者 ZYL", Toast.LENGTH_LONG);
        				toast.show();
        			}
        		});
    }
    
   
    private void initResource(){
    	if(new DBHelper(Main.this).getCount()==0){
       	  
      
       try {
    	   InputStream templateStream = getResources().openRawResource(R.raw.template);
			FileUtil.createFile(templateStream,Environment.getExternalStorageDirectory().getPath());
		} catch (IOException e) {
            System.out.println("main error  FileUtil.createFile");			 
		}
		 InputStream stream = getResources().openRawResource(R.raw.points); 
       	  for(Question q:FileUtil.create(stream)){
       		new DBHelper(Main.this).insert(q) ;
       	  }
      	}
    }
}